package com.vitabuybuy.tv.ui.shopcenter.viewmodel

import androidx.databinding.ObservableArrayList
import androidx.lifecycle.MutableLiveData
import com.library.common.base.BaseViewModel
import com.library.common.base.MessageModel
import com.library.common.ext.rxHttpRequest
import com.library.common.net.error.msg
import com.library.common.net.loadingtype.LoadingType
import com.library.common.toast.XToast
import com.vitabuybuy.tv.BR
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.dto.*
import com.vitabuybuy.tv.inter.OnGoodItemClickListener
import com.vitabuybuy.tv.inter.OnItemStorelickListener
import com.vitabuybuy.tv.network.AddressRepository
import com.vitabuybuy.tv.network.CouponCenterRepository
import com.vitabuybuy.tv.network.ShopCenterRepository
import me.tatarka.bindingcollectionadapter2.ItemBinding
import me.tatarka.bindingcollectionadapter2.OnItemBind

class SearchGoodsOrStoreViewModel : BaseViewModel() {

    private val itemOnGoodsClickListener = object : OnGoodItemClickListener {
        override fun onItemClick(item: GoodsDto, position: Int) {
            defUI.msgEvent.postValue(MessageModel(2, obj = item))
        }
    }

    private val itemOnStoreClickListener = object : OnItemStorelickListener {
        override fun onItemClick(item: StoreListDto, position: Int) {
            defUI.msgEvent.postValue(MessageModel(2, obj = item))
        }
    }

    val goodlist = ObservableArrayList<GoodsDto>()

    //网格布局的adapter绑定
    val goodsBinding: OnItemBind<GoodsDto> =
        OnItemBind { itemBinding: ItemBinding<Any>, position: Int, item: GoodsDto ->
            itemBinding.set(BR.itemBean, R.layout.item_goods).bindExtra(BR.position, position)
                .bindExtra(BR.listenner, itemOnGoodsClickListener)
        }

    val storelist = ObservableArrayList<StoreListDto>()

    //网格布局的adapter绑定
    val storeBinding: OnItemBind<StoreListDto> =
        OnItemBind { itemBinding: ItemBinding<Any>, position: Int, item: StoreListDto ->
            itemBinding.set(BR.itemBean, R.layout.item_store_by_list)
                .bindExtra(BR.position, position)
                .bindExtra(BR.listenner, itemOnStoreClickListener)
        }


    fun getGoodsList(content: Any, storeId: Int?, couponId: Int?, sort: String, pageNumber: Int) {
        rxHttpRequest {
            onRequest = {
                val it = ShopCenterRepository.getCateSearchGoodList(
                    content,
                    storeId,
                    couponId,
                    sort,
                    pageNumber,
                    10
                ).await()
                if (pageNumber == 1) {
                    goodlist.clear()
                }
                goodlist.addAll(it)
                if (goodlist.isEmpty()) {
                    statusView.set(LoadingType.SHOW_EMPTY)
                }else{
                    statusView.set(LoadingType.SHOW_CONTENT)
                }
                defUI.msgEvent.postValue(MessageModel(1, obj = it.size))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isLoadingType = true
            isRefresh = pageNumber == 1
        }
    }

    fun getStoreList(content: String, pageNumber: Int) {
        rxHttpRequest {
            onRequest = {
                val it = ShopCenterRepository.searchStoreList(content, pageNumber).await()
                if (pageNumber == 1) {
                    storelist.clear()
                }
                storelist.addAll(it)
                if (storelist.isEmpty()) {
                    statusView.set(LoadingType.SHOW_EMPTY)
                }else{
                    statusView.set(LoadingType.SHOW_CONTENT)
                }
                defUI.msgEvent.postValue(MessageModel(1, obj = it.size))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isLoadingType = true
            isRefresh = pageNumber == 1
        }
    }

    val brandList = MutableLiveData<MutableList<BrandDto>>()
    fun getAllBrand() {
        rxHttpRequest {
            onRequest = {
                brandList.value = ShopCenterRepository.getAllBrand().await()
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }

    val hotKeylist = MutableLiveData<MutableList<HotKeyDto>>()
    fun gethotKey() {
        rxHttpRequest {
            onRequest = {
                hotKeylist.value = ShopCenterRepository.gethotKey().await()
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }


    fun getScreenGoods(
        brandList2: MutableList<BrandDto>,
        salePriceDto: SalePriceDto,
        sort: String,
        keyWord: Any,
        pageNumber: Int,
        regionId: Int?
    ) {
        rxHttpRequest {
            onRequest = {
                val it = ShopCenterRepository.getScreenGoodList(
                    brandList2,
                    salePriceDto,
                    sort,
                    keyWord,
                    pageNumber,
                    10,
                    regionId!!
                ).await()
                if (pageNumber == 1) {
                    goodlist.clear()
                }
                goodlist.addAll(it)
                if (goodlist.isEmpty()) {
                    statusView.set(LoadingType.SHOW_EMPTY)
                }else{
                    statusView.set(LoadingType.SHOW_CONTENT)
                }
                defUI.msgEvent.postValue(MessageModel(1, obj = it.size))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isLoadingType = true
            isRefresh = pageNumber == 1
        }
    }

    fun getRegionList(regionId: Int?) {
        rxHttpRequest {
            onRequest = {
                val it = AddressRepository.getRegionList(regionId).await()
                defUI.msgEvent.postValue(MessageModel(3, obj = it))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }
}
